<?php

namespace Api\Controller;
use Think\Controller;

class IndexController extends HomeController
{


    //获取地址
    public function getAddress(){
        I('get.district')? $where['name']=array('like',"%".I('get.district')."%"): $this->ajaxReturn(['status'=>0,'info'=>"地址不存在"]);
        $where['status']=1;
        $re=M('address')->where($where)->find();
        if(!empty($re)){
            $this->ajaxReturn(['status'=>1,'info'=>$re]);
        }else{
            $this->ajaxReturn(['status'=>0,'info'=>"地区没开通"]);
        }
    }

    //获取分类
    public function Cate()
    {
        $list=M('cate')->order('id asc')->select();
        $this->ajaxReturn(['status'=>1,'items'=>$list]);
    }

    public function info(){
        $p=I('p')?I('p'):0;

        I('cate')? $where['cate_id']=I('cate'):'';
        $district_id=I('district_id');

        $word=I('word');
        if(!empty($word)){
            $where['des']=array('like',"%{$word}%");
        }

        $where['_string'] = '(district_id='.$district_id.' AND feeType=0 )  OR  ( district_id='.$district_id.' AND feeType > 0 AND  end_time < '.time().') AND status=1';

        $count=M('info')->where($where)->count();
        if(($p+1)*10<$count){
            $list=M('info')->where($where)->order('create_time desc')->limit($p*10,10)->select();
        }else{
            $list=M('info')->where($where)->order('create_time desc')->limit(10)->select();
        } 



        //置顶的数据
        $map['feeType']=array('gt',0);
        $map['end_time']=array('gt',time());
        $map['district_id']=$district_id;
        $map['cate_id']=$where['cate_id'];
        $map['status']=1;
        $top=M('info')->where($map)->order('create_time desc')->limit($p*10,10)->select();

        $toplist=array();
        $listarr=array();
        $catelist=M('cate')->select();
        $catelist=get_id_key($catelist,'id');
        foreach ($top as $k=>$v){
            $item['id']=$v['id'];
            $item['cate']=$catelist[$v['cate_id']]['name']?$catelist[$v['cate_id']]['name']:"无";
            $item['msg']=msubstr($v['des'],0,7);
            $item['reword']=$v['money'];
            $item['date']=M('feetype')->where(array('id'=>$v['feeType']))->getField('date');
            $item['time']=(($v['create_time']+3600)>time())?"刚刚":date('Y-m-d',$v['create_time']);
            $toplist[]=$item;
        }
        foreach ($list as $k=>$v){
            $item['id']=$v['id'];
            $item['cate']=$catelist[$v['cate_id']]['name']?$catelist[$v['cate_id']]['name']:"无";
            $item['msg']=msubstr($v['des'],0,7);
            $item['feeType']=$v['feeType'];
            $item['reword']=$v['money'];
            $item['time']=(($v['create_time']+3600)>time())?"刚刚":date('Y-m-d',$v['create_time']);
            $listarr[]=$item;
        }
        $adds=M('address')->where(array('status'=>1))->select();

        $this->ajaxReturn(['status'=>1,'top'=>$toplist,'list'=>$listarr,'adds'=>$adds]);
    }

    public function detail(){
        I('get.id')? $where['id']=I('get.id'): $this->ajaxReturn(['status'=>0,'info'=>"信息不存在"]);
        $info=M('info')->where($where)->find();
        if(!empty($info)){
            $user=M('agent')->where(array('uid'=>$info['user_id']))->field('nickname,avatar')->find();
            $return['username']=$user['nickname'];
            $return['head']=$user['avatar'];
            $return['time']=date('Y-m-d H:i',$info['create_time']);
            $return['title']=M('cate')->where(array('id'=>$info['cate_id']))->getField('name');
            $return['content']=$info['content'];
            $return['imgList']=explode(",",$info['img']);
            $return['name']=$info['name'];
            $return['mobile']=$info['mobile'];
            $return['address']=$info['address'];
            $return['feeType']=$info['feeType'];
            $return['money']=$info['money'];
            $return['comments']=M('comment')
                                ->where(array('info_id'=>$where['id']))
                                ->field('id,info_id,uid,star,content,create_time,status,isAnonymous,image')
                                ->order('create_time desc')->select();
            $return['showadpot']=true;
            $return['adpot_id']=0;
            foreach ($return['comments'] as $key => &$vo) {
                $vo['user']=M('agent')->where(array('uid'=>$vo['uid']))->field('nickname,avatar')->find();
                $vo['create_time']=date('Y-m-d H:i:s',$vo['create_time']);
                if($vo['status']==1){
                    $return['showadpot']=false;
                    $return['adpot_id']=$vo['id'];
                }
            }
            $this->ajaxReturn(['status'=>1,'info'=>$return]);
        }else{
            $this->ajaxReturn(['status'=>0,'info'=>"信息不存在"]);
        }
    }


    //置顶信息
    public function feeType(){
        $list=M('feetype')->select();
        $list=get_id_key($list,'id');
        $prompt=M('config')->where(array('name'=>'IS_OPEN_PROMPT'))->find();
        $publish_btn=__ROOT__ . '/Public/Api/images/publish_btn.png';
        $this->ajaxReturn(['status'=>1,'items'=>$list,'flag'=>$prompt['value'],'publish_btn'=>$publish_btn]);
    }

    //发布信息
    public function saveInfo(){
        if(IS_POST){
            $data=I('post.');
            $uid=is_wxlogin($data['session_key']);
            if($uid){
                $user=M('agent')->where(array('uid'=>$uid))->find();
                if(empty($user)){
                    $this->ajaxReturn(['status'=>0,'info'=>"用户不存在"]);
                    die();
                }
                if($user['status']!=1){
                    $this->ajaxReturn(['status'=>0,'info'=>"当前用户已被封号"]);
                    die();
                }
                if(mb_strlen($data['des'])>20){
                    $this->ajaxReturn(['status'=>0,'info'=>"摘要长度为20个字符"]);
                    die();
                }

                if(mb_strlen($data['title'])>10){
                    $this->ajaxReturn(['status'=>0,'info'=>"标题长度为10个字符"]);
                    die();
                }

                if(mb_strlen($data['des'])>20){
                    $this->ajaxReturn(['status'=>0,'info'=>"摘要长度为20个字符"]);
                    die();
                }
                if($user['balance']<$data['money']){
                    $this->ajaxReturn(['status'=>0,'info'=>"账户余额不足"]);
                    die();
                }
                $config=M('config')->where(array('name'=>'SENSITIVE_KEYWORD'))->find();
                $row=filter_Sensitive($data['content'], explode(",",$config['value']));

                $shenhe=M('config')->where(array('name'=>'IS_OPEN_SHENHE'))->find();

                if($shenhe['value'] || $row){
                    $data['status']=0;
                }else{
                    $data['status']=1;
                }
                if($data['feeType']>0){
                    $feeType=M('feetype')->where(array('id'=>$data['feeType']))->find();
                    $data['end_time']=strtotime('+'.$feeType['date'].' day');
                }
                $data['create_time']=time();
                $data['user_id']=$uid;
                $data['des']=msubstr($data['content'],0,20);
                //开启事务
                M()->startTrans();

                $re=M('info')->add($data);
                $data['money']=empty($data['money'])?0:$data['money'];
                $re2=M('agent')->where(array('uid'=>$uid))->setDec('balance',$data['money']);

                if($re!==false && $re2!==false){
                    //事务提交
                    M()->commit();
                    $this->ajaxReturn(['status'=>1,'info'=>"发布成功"]);
                }else{
                    //事务回滚
                    M()->rollback();
                    $this->ajaxReturn(['status'=>0,'info'=>"服务器出错，请稍后重试"]);
                }
            }else{
                $this->ajaxReturn(['status'=>0,'info'=>"用户没有登录"]);
            }

        }

    }


    public function submitComment(){
        if(IS_POST){
            $data=I('post.');
            $uid=is_wxlogin($data['session_key']);
            if($uid){
                $info=M('info')->where(array('id'=>$data['info_id']))->field('id,user_id')->find();
                if($info['user_id']==$uid){
                    $this->ajaxReturn(['status'=>0,'info'=>"不能评论自己的帖子"]);
                    die();
                }
                $user=M('agent')->where(array('uid'=>$uid))->find();
                if(empty($user)){
                    $this->ajaxReturn(['status'=>0,'info'=>"用户不存在"]);
                    die();
                }
                unset($data['session_key']);
                $data['create_time']=time();
                $data['uid']=$uid;
                $data['status']=0;
                $re=M('comment')->add($data);
                if($re!==false){
                    $re2=D('Webinfo')->send(['title'=>'你发布的帖子收到评论，请立即查看','receiver_id'=>$info['user_id'],'content'=>'你发布的帖子收到评论，请立即查看','cate'=>2]);
                    $this->ajaxReturn(['status'=>1,'info'=>"发布成功"]);
                }else{
                    $this->ajaxReturn(['status'=>0,'info'=>"服务器出错，请稍后重试"]);
                }
            }else{
                $this->ajaxReturn(['status'=>0,'info'=>"用户没有登录"]);
            }
       }     
    }


    //采纳
    public function Adopt(){
        if(IS_POST){
            $data=I('post.');
            $uid=is_wxlogin($data['session_key']);
            if($uid){
                //帖子信息
                $info=M('info')->where(array('id'=>$data['info_id']))->field('id,user_id,money,feeType')->find();
                if($info['user_id']!=$uid){
                    $this->ajaxReturn(['status'=>0,'info'=>"只有发帖人能采纳"]);
                    die();
                }
                $user=M('agent')->where(array('uid'=>$uid))->find();
                if(empty($user)){
                    $this->ajaxReturn(['status'=>0,'info'=>"用户不存在"]);
                    die();
                }

                $model=M('comment');
                $comment=$model->where(array('id'=>$data['comment_id']))->find();

                //开启事务
                M()->startTrans();
                //修改评论状态
                $re1=$model->where(array('id'=>$data['comment_id']))->setField('status',1);
                if($info['feeType']==0){
                    //划归赏金
                    $re2=M('agent')->where(array('uid'=>$comment['uid']))->setInc('balance',$info['money']);
                }else{
                    $re2=true;
                }

                if($re1!==false && $re2!==false ){
                    M()->commit();
                    $this->ajaxReturn(['status'=>1,'info'=>"发布成功"]);
                }else{
                    M()->rollback();
                    $this->ajaxReturn(['status'=>0,'info'=>"服务器出错，请稍后重试"]);
                }
            }else{
                $this->ajaxReturn(['status'=>0,'info'=>"用户没有登录"]);
            }
        }
    }





}